<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');

class JVController extends JController {
    
    public $model;
    public $_model;
    public $_option;
    public $_link;

    function __construct($default = array()) {
        parent::__construct($default); 
        $this->_model  =& $this->getModel($this->model); 
              
        $this->registerTask('unpublish','publish');
        $this->registerTask('add','edit');
        $this->registerTask('apply','save');
    }
    
    function _setModel($model){
        $this->model = $model;
        return true;
    }
    
    function _setLink($link){
        $mainframe     = JFactory::getApplication();        
        $this->_option = $mainframe->get('scope'); 
        $this->_link = 'index.php?option='.$this->_option.$link;        
        return true;
    }

    function display() {
        $view = JRequest::getVar("view");

        if (empty($view)) {
            JRequest::setVar("view", "default");
        }
        parent::display();
    }

    function displays($view = 'default', $layout = 'edit') {
        $views = $this->getView($view, 'html');
        $views->setModel($this->getModel($view), true);
        $views->setLayout($layout);
        $views->$layout();
    }
    
    function save(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        $date = JFactory::getDate();
        $data = JRequest::get('post');
        $task = JRequest::getVar('task'); 
        
        if(!$this->_model -> store($data)){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        $id = $this->_model->getId(); 
        
        $link = $task == 'save' ? $this->_link : $this->_link.'&task=edit&cid[]='.$id;     
        $this->setRedirect( $link, $msg );
    }
    
    function publish(){ 
        JRequest::checkToken () or jexit ( 'Invalid Token' );
                
        if(!$this->_model -> publish()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
              
        $this->setRedirect( $this->_link, $msg );
    }
    
    function orderdown() {
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        
        if(!$this->_model -> order(1)){
            $msg = ERROR_MESSAGE;
        } else {
            $msg = SAVE_MESSAGE;
        }
       
        $this->setRedirect ( $this->_link, $msg );
    }
    
    function orderup() {
        JRequest::checkToken () or jexit ( 'Invalid Token' );
               
        if(!$this->_model -> order(-1)){
             $msg = ERROR_MESSAGE;
        } else {
             $msg = SAVE_MESSAGE;
        } 
        
        $this->setRedirect ( $this->_link, $msg );
    }
    
    function saveorder() {        
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        
        if(!$this->_model -> saveOrder()){
            $msg = ERROR_MESSAGE;
        } else {
            $msg = SAVE_MESSAGE;
        } 
       
        $this->setRedirect ( $this->_link, $msg );
    } 
              
    function cancel() {
        $msg = CANCEL_MESSAGE;
        $this->setRedirect ( $this->_link, $msg );
    } 
    
    function remove(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        
        if(!$this->_model -> delete()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = REMOVED;
        }
        
        $this->setRedirect ( $this->_link, $msg );
    }
}
